package com.wangxiang.shop.member.controller;

import java.util.*;


import com.wangxiang.common.utils.MyStringUtile;
import com.wangxiang.common.utils.PageEntity;
import com.wangxiang.shop.member.entity.*;
import com.wangxiang.shop.member.feign.ProductFeign;
import com.wangxiang.shop.member.service.MemberService;



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.wangxiang.shop.member.service.ShopCartService;
import com.wangxiang.common.utils.R;



/**
 * 
 *
 * @author wangxiang
 * @email 2293672863@qq.com
 * @date 2022-04-05 22:22:19
 */
@RestController
@RequestMapping("member/shopcart")
public class ShopCartController {
    @Autowired
    private ShopCartService shopCartService;
    @Autowired
    ProductFeign productFeign;
    @Autowired
    private MemberService memberService;

    @RequestMapping("add")
    public R add(@RequestBody HashMap<String , String> map){
        List<Integer> skuIdList = new ArrayList<>();
        if (map.get("skuIdList")!=null){
            MyStringUtile myStringUtile = new MyStringUtile();
            List<Integer> skuIdList1 = myStringUtile.skuIdStrChangeSkuIdList(map.get("skuIdList").toString());
            skuIdList = skuIdList1;
        }
        List<SkuEntity> skuList = productFeign.getSkuList(skuIdList);


        int add = shopCartService.add(map, skuList);
        if (add == 0){
            return R.error("系统错误添加失败");
        }
        return R.ok().put("data",add);
    }
    @RequestMapping("list")
    public R list(@RequestBody HashMap<String , Integer> map){
        int userId = map.get("userId");
        List<ShopCartEntity> list = shopCartService.list(userId);
        if (list.size()==0){
            return R.ok().put("msg","该用户还没有订购商品").put("code",700);
        }
        List<MemberCartEntity> memberCartEntities = productFeign.MemberCart(list);

        return R.ok().put("data",memberCartEntities);
    }


    @RequestMapping("page")
    public R adminCart(@RequestBody PageEntity page){
        PageEntity page1 = shopCartService.page(page);
        List<CartEntity> cartEntity = shopCartService.getCartEntity((List<ShopCartEntity>) page1.getList());
        List<CartEntity> membercart = productFeign.membercart(cartEntity);
        return R.ok().put("list",membercart).put("page",page1);
    }

    @RequestMapping("search")
    public R search(@RequestBody HashMap<String,Object> map){
        String nullStr = "";
        if (map.get("search")==null){
            return R.error("请输入再查询");
        }
        if (nullStr.equals(map.get("search"))){
            return R.error("请输入再查询");
        }
        //查询用户名
        MemberEntity memberEntity = memberService.getEntityByName(map.get("search").toString());
        if (memberEntity==null){
            return R.error("用户不存在");
        }
        PageEntity search = shopCartService.search(map);
        if (search==null){
            return R.error("用户不存在或用户购物车并没有商品");
        }

        List<CartEntity> cartEntity = shopCartService.getCartEntityBySearch((List<ShopCartEntity>) search.getList(),memberEntity.getName());
        List<CartEntity> membercart = productFeign.membercart(cartEntity);
        //未知重复,排重
        ArrayList<CartEntity> list = new ArrayList<>();
        if (membercart.size()!=0){
            for (CartEntity entity : membercart) {
                if (!list.contains(entity)){
                    list.add(entity);
                }
            }
        }
        if (cartEntity.size()==0){
            return R.error("用户购物车没有数据");
        }


        return R.ok().put("list",list).put("page",search);

    }



}
